import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_base/common/commonColors.dart';
import 'package:flutter_base/common/theme.dart';
import 'package:get/get.dart';

/// Create： Zhang 3/18/21 11:32 AM
/// 描述：页面的基础类
class PageStateless<Logic extends GetxController> extends StatelessWidget {
  ///newInstance:
  ///是否缓存controller实例,false为缓存到内存中,作用是可跨页面共享数据,
  ///true为每次创建新的实例,作用是大多数时候,重新打开一个页面数据需要重置
  PageStateless({
    this.title = "标题",
    this.titleColor = Colors.black87,
    this.backgroundColor = defaultBgColor,
    newInstance = true,
    Logic? logic,
    Key? key,
  }) : super(key: key) {
    if (logic != null) {
      if (newInstance && Get.isRegistered<Logic>()) {
        Get.delete<Logic>();
      }
      Get.put(logic);
    }

    initData();
  }

  final String title; //标题
  final Color titleColor; //标题文字颜色
  final Color backgroundColor; //页面背景色

  ///获取controller示例
  Logic get pageLogic => GetInstance().find<Logic>();

  ///创建页面
  @override
  Widget build(BuildContext context) {
    setTranStatusBar(context);
    return createBody(context);
  }

  ///数据初始化,非可靠方法,有时候build方法会执行多次
  void initData() {}

  ///创建body,优先使用rootView
  Widget createBody(BuildContext context) {
    return rootView(context) ?? defaultWidget(context);
  }

  ///覆写此方法则覆盖所有主题,与其方法互斥
  @protected
  Widget? rootView(BuildContext context) => null;

  ///使用默认主题,带有appBar,设置页面内容
  @protected
  Widget? content(BuildContext context) => null;

  ///覆写此方法,覆盖默认的titleBar返回按钮
  @protected
  Widget? leading(BuildContext context) => null;

  ///覆写此方法,添加底部导航栏
  @protected
  Widget? navigation(BuildContext context) => null;

  ///覆写此方法添加tabBar
  @protected
  PreferredSizeWidget? tabBar(BuildContext context) => null;

  ///覆写此方法,添加titleBar右侧action按钮
  @protected
  List<Widget>? actions(BuildContext context) => [];

  ///关闭应用
  void close(BuildContext context) {
    if (Theme.of(context).platform == TargetPlatform.android) {
      //退出flutter应用,相当于小程序的关闭按钮
      SystemNavigator.pop();
    } else {
      exit(0);
    }
  }

  ///返回上一页
  void goBack(BuildContext context, {Object? result}) {
    //不是首页返回上一页
    Navigator.pop(context, result);
  }

  ///默认界面控件
  Widget defaultWidget(BuildContext context) => Scaffold(
        appBar: AppBar(
          backgroundColor: backgroundColor,
          centerTitle: true,
          //设置状态栏字体为黑色
          brightness: Brightness.light,
          elevation: 0,
          title: Text(
            title,
            style: TextStyle(
              color: Colors.black,
              fontSize: 17,
              fontWeight: FontWeight.bold,
            ),
          ),
          leading: leading(context) ??
              IconButton(
                color: titleColor,
                icon: Icon(
                  Icons.arrow_back_ios_sharp,
                  size: 20,
                ),
                onPressed: () {
                  goBack(context);
                },
              ),
          actions: actions(context),
          bottom: tabBar(context),
        ),
        body: content(context),
        bottomNavigationBar: navigation(context),
        backgroundColor: backgroundColor,
      );
}
